//
//  ScenicDetailNavBar.swift
//  LeTu
//
//  Created by g1 on 2024/1/12.
//

import Foundation

class ScenicDetailNavBar: UIView {
    
    var backHandle: (()->Void)?
    
    init(title: String) {
        super.init(frame: .zero)
        
        configUI()
        titleLabel.text = title
    }
    func changeNaviBarUIStatus(_ offsetY: CGFloat) {
        
        let imgHeight = ScenicDetailHeadView.getHeadImgViewHeight() - Size.navigationBarHeight
        
        var topOfsetY = abs(offsetY)
        if topOfsetY <= imgHeight/2 {
            topOfsetY = imgHeight/2 - topOfsetY
            //                MYLog("减小",topOfsetY,imgHeight/2)
            backBtn.setImage(UIImage(named: "back_white"))
            //shareBtn.setImage(UIImage(named: "service_org_detail_share_icon"))
            backView.alpha = 0.0
            titleLabel.alpha = 0.0
            backBtn.alpha = topOfsetY / (imgHeight/2)
            shareBtn.alpha = backBtn.alpha
            shareBtn.setImageEdgeInsets(UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 16))
        } else {
            topOfsetY = topOfsetY - imgHeight/2
            //                MYLog("增加",topOfsetY,imgHeight/2)
            backBtn.setImage(UIImage(named: "back_pop_black"))
            //shareBtn.setImage(UIImage(named: "service_org_detail_share_navi_icon"), for: .normal)
            backView.alpha = topOfsetY / (imgHeight/2)
            titleLabel.alpha = backView.alpha
            backBtn.alpha = backView.alpha
            shareBtn.alpha = backBtn.alpha
            shareBtn.setImageEdgeInsets(UIEdgeInsets(top: 9, left: 1, bottom: 9, right: 17))
        }

    }
    func setTitle(_ title: String) {
        titleLabel.text = title
    }
    
    // MARK: - UIButtonAction
    @objc func backBtnAction() {
        guard let handle = backHandle else { return }
        handle()
    }
    @objc func shareBtnAction() {
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        addSubview(defaultBarView)
        defaultBarView.addSubview(backBtn)
        defaultBarView.addSubview(shareBtn)
        defaultBarView.addSubview(titleLabel)
        
        let barHeight = 32+16
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        defaultBarView.snp.makeConstraints { make in
            make.top.equalTo(Size.statusBarHeight)
            make.height.equalTo(barHeight)
            make.left.right.equalToSuperview()
        }
        backBtn.snp.makeConstraints { make in
            make.left.centerY.height.equalToSuperview()
            make.width.equalTo(barHeight)
        }
        shareBtn.snp.makeConstraints { make in
            make.right.centerY.height.equalToSuperview()
            make.width.equalTo(barHeight)
        }
        titleLabel.snp.makeConstraints { make in
            make.right.equalTo(shareBtn.snp.left).offset(-12)
            make.left.equalTo(backBtn.snp.right).offset(12)
            make.centerY.equalToSuperview()
        }
        
        backBtn.addTarget(self, action: #selector(backBtnAction))
        shareBtn.addTarget(self, action: #selector(shareBtnAction))
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        backView.backgroundColor = .app_viewBackgroundColor
        backView.alpha = 0.0
        return backView
    }()
    private lazy var defaultBarView: UIView = {
        let backView = UIView()
        return backView
    }()
    private lazy var backBtn: CustomClickableView = {
        let backBtn = CustomClickableView()
        backBtn.setImage(UIImage(named: "back_gray"))
        backBtn.setImageEdgeInsets(UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 0))
        return backBtn
    }()
    private lazy var shareBtn: CustomClickableView = {
        let shareBtn = CustomClickableView()
        shareBtn.setImage(UIImage(named: "service_org_detail_share_icon"))
        shareBtn.setImageEdgeInsets(UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 16))
        return shareBtn
    }()
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangMedium(17)
        titleLabel.textAlignment = .center
        titleLabel.alpha = 0
        return titleLabel
    }()
        
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
